package com.le.tester.imooc.masterJUC.lock.reentrantlock;

import java.util.PrimitiveIterator;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * 描述：递归调用锁
 */
public class RecursionDemo {

    //核心使用了AQS,默认的是一个非公平锁
    private static ReentrantLock lock = new ReentrantLock();


    public static void main(String[] args) {
        //当前线程是不是持有这把锁住
        //lock.isHeldByCurrentThread();
        //等待这把锁的队列有多长
        //lock.getQueueLength();
        accessResource();
    }

    public static void accessResource() {
        lock.lock();
        System.out.println("已经对资源进行了处理");
        try {
            //使用递归，直接锁了五次
            if (lock.getHoldCount() < 5) {
                System.out.println(lock.getHoldCount());
                System.out.println("是不是持有这把锁：" + lock.isHeldByCurrentThread());
                System.out.println("等待这把锁的队列：" + lock.getQueueLength());
                accessResource();
                System.out.println(lock.getHoldCount());
            }
        } finally {
            lock.unlock();
            System.out.println("是不是持有这把锁：" + lock.isHeldByCurrentThread());
        }

    }
}
